//
//  RankViewController.m
//  Japanese+
//
//  Created by Jame on 15/10/24.
//  Copyright © 2015年 SSIC. All rights reserved.
//

#import "RankViewController.h"
#import "RankCell.h"
#import "LateCell.h"

#import "RankModel.h"
#import "InfoViewController.h"

@interface RankViewController () <UITableViewDataSource,UITableViewDelegate>

@end

@implementation RankViewController
{
    UserInfoManager *_userInfo;
    UIView *_chooseView;
    UITableView *_tableView;
    MemberModel *_memberModel;
    
    RankModel *_rankModel;
    NSInteger _index;
    NSMutableArray *_dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatNavigationBarWithImage:nil title:@"教师综合排名榜"];
    [self creatNavigationBarLeftItemWithLeftTitle:nil LeftImage:ECIMAGENAME(@"back_button.png")];

    
    _userInfo = [UserInfoManager sharedUserInfoManager];
    [_userInfo loadUserInfoFromSanbox];
    [self creatChooseView];
    [self creatTableView];
}


- (void)viewWillAppear:(BOOL)animated
{
    [self loadUserInfo];
    UIButton *rankButton = (UIButton *)[self.view viewWithTag:105];
    [self chooseClick:rankButton];
}


- (void)leftBtnClick:(id)leftSender
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)loadUserInfo
{
    UserInfoManager *userInfo = [UserInfoManager sharedUserInfoManager];
    [userInfo loadUserInfoFromSanbox];
    _memberModel = userInfo.memberModel;
}

- (void)creatHttpRequest
{
    NSDictionary *param = @{
                            @"member_Id":_memberModel.member_Id,
                            @"pageIndex":[NSString stringWithFormat:@"%ld",_index],
                            @"pageSize":@"10",
                            };
    
    [[HttpManager defaultManager] postRequestToUrl:RequestURL(RANK) params:param complete:^(BOOL successed, NSDictionary *result) {
        if (successed) {
            [RankModel setupObjectClassInArray:^NSDictionary *{
                return @{
                         @"data":@"RankData",
                         };
            }];
            
            if (_dataArray) {
                if (_tableView.mj_header.isRefreshing) {
                    [_dataArray removeAllObjects];
                }
            }else{
                _dataArray = [[NSMutableArray alloc] init];
            }
            _rankModel = [[RankModel alloc] init];
            _rankModel = [RankModel objectWithKeyValues:result];
            
            [_dataArray addObjectsFromArray:_rankModel.data];
            [_tableView reloadData];
            
            [_tableView.mj_header endRefreshing];
            [_tableView.mj_footer endRefreshing];
        }else{
            [_tableView.mj_header endRefreshing];
            [_tableView.mj_footer endRefreshing];
        }
    }];
}


- (void)creatChooseView
{
    _chooseView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, ECSCREEN_W, 40)];
    _chooseView.backgroundColor = ECCOLOR(255, 255, 255, 1);
    [self.view addSubview:_chooseView];
    
    
    UIButton *rankButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rankButton.tag = 105;
    rankButton.frame = CGRectMake(0, 0, ECSCREEN_W/2, 40);
    [rankButton setTitle:@"综合排名" forState:UIControlStateNormal];
    [rankButton setTitleColor:ECCOLOR(0, 0, 0, 1) forState:UIControlStateNormal];
    rankButton.titleLabel.font = [UIFont systemFontOfSize:15.f];
    [rankButton setImage:ECIMAGENAME(@"order_down.png") forState:UIControlStateNormal];
    rankButton.imageEdgeInsets = UIEdgeInsetsMake(0, 145, 0, 0);
    [rankButton addTarget:self action:@selector(chooseClick:) forControlEvents:UIControlEventTouchUpInside];
    [_chooseView addSubview:rankButton];
    
    UIView *speraLine = [[UIView alloc] initWithFrame:CGRectMake(ECSCREEN_W/2, 5, 1, 30)];
    speraLine.backgroundColor = ECCOLOR(212, 50, 47, .3f);
    [_chooseView addSubview:speraLine];
    
    
    UIButton *lateButton = [UIButton buttonWithType:UIButtonTypeCustom];
    lateButton.tag = 106;
    lateButton.frame = CGRectMake(ECSCREEN_W/2+1, 0, ECSCREEN_W/2, 40);
    [lateButton setTitle:@"延误次数" forState:UIControlStateNormal];
    [lateButton setTitleColor:ECCOLOR(0, 0, 0, 1) forState:UIControlStateNormal];
    lateButton.titleLabel.font = [UIFont systemFontOfSize:15.f];
    [lateButton setImage:ECIMAGENAME(@"order_down.png") forState:UIControlStateNormal];
    lateButton.imageEdgeInsets = UIEdgeInsetsMake(0, 145, 0, 0);
    [lateButton addTarget:self action:@selector(chooseClick:) forControlEvents:UIControlEventTouchUpInside];
    [_chooseView addSubview:lateButton];
}



- (void)chooseClick:(UIButton *)button
{
    [_tableView.mj_header beginRefreshing];
    
    UIButton *rankButton = (UIButton *)[self.view viewWithTag:105];
    UIButton *lateButton = (UIButton *)[self.view viewWithTag:106];
    
    switch (button.tag) {
        case 105:
        {
            rankButton.selected = YES;
            lateButton.selected = NO;
            [rankButton setTitleColor:NAVC_COLOR forState:UIControlStateNormal];
            [lateButton setTitleColor:ECCOLOR(0, 0, 0, 1) forState:UIControlStateNormal];
            [rankButton setImage:ECIMAGENAME(@"order_down.png") forState:UIControlStateNormal];
            [lateButton setImage:ECIMAGENAME(@"order_up.png") forState:UIControlStateNormal];
        }
            break;
        case 106:
        {
            rankButton.selected = NO;
            lateButton.selected = YES;
            [rankButton setTitleColor:ECCOLOR(0, 0, 0, 1) forState:UIControlStateNormal];
            [lateButton setTitleColor:NAVC_COLOR forState:UIControlStateNormal];
            [rankButton setImage:ECIMAGENAME(@"order_up.png") forState:UIControlStateNormal];
            [lateButton setImage:ECIMAGENAME(@"order_down.png") forState:UIControlStateNormal];
        }
            break;
            
            
        default:
            break;
    }
}


- (void)creatTableView
{
    self.automaticallyAdjustsScrollViewInsets = YES;
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 104, ECSCREEN_W, ECSCREEN_H - 104) style:UITableViewStylePlain];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    
    _tableView.tableFooterView = [[UIView alloc]init];
    
    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [_tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [_tableView setLayoutMargins:UIEdgeInsetsZero];
    }

    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefreshing)];
    _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefreshing)];
}


- (void)headerRefreshing
{
    _index = 0;
    [self creatHttpRequest];
}

- (void)footerRefreshing
{
    if (_dataArray.count%10 == 0) {
        _index ++;
        [self creatHttpRequest];
    }else{
        [_tableView.mj_footer endRefreshingWithNoMoreData];
    }
}


#pragma mark - tableViewNumberOfRowsInSection
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}


#pragma mark - tableViewHeightForRowAtIndexPath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90.f;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5.f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ECSCREEN_W, 5)];
    view.backgroundColor = BG_COLOR;
    return view;
}


#pragma mark - tableViewCellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIButton *button = (UIButton *)[self.view viewWithTag:105];
    if (button.selected) {
        static NSString *reuseIdentifier = @"rankCell";
        RankCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
        if (nil == cell) {
            NSArray *arr = [[NSBundle mainBundle]loadNibNamed:@"RankCell" owner:nil options:nil];
            for (NSObject *obj in arr) {
                if ([obj isKindOfClass:[RankCell class]]) {
                    cell = (RankCell *)obj;
                    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
                        [cell setSeparatorInset:UIEdgeInsetsZero];
                    }
                    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
                        [cell setLayoutMargins:UIEdgeInsetsZero];
                    }
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    break;
                }
            }
        }
        [cell setCellWithModel:_dataArray WithIndexPath:indexPath];
        return cell;
        
    }else{
        static NSString *reuseIdentifier = @"lateCell";
        LateCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
        if (nil == cell) {
            NSArray *arr = [[NSBundle mainBundle]loadNibNamed:@"LateCell" owner:nil options:nil];
            for (NSObject *obj in arr) {
                if ([obj isKindOfClass:[LateCell class]]) {
                    cell = (LateCell *)obj;
                    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
                        [cell setSeparatorInset:UIEdgeInsetsZero];
                    }
                    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
                        [cell setLayoutMargins:UIEdgeInsetsZero];
                    }
                    
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    break;
                }
            }
        }
        
        [cell setCellWithModel:_dataArray WithIndexPath:indexPath];
        return cell;
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RankData *data = _dataArray[indexPath.section];
    
    NSDictionary *param = @{
                            @"teacher_Id":data.member_Id,
                            @"member_Id":_userInfo.memberModel.member_Id,
                            };
    
    [[HttpManager defaultManager] postRequestToUrl:RequestURL(CREATSCAN) params:param complete:^(BOOL successed, NSDictionary *result) {
        
    }];
    
    InfoViewController *vc = [[InfoViewController alloc] init];
    vc.memberId = data.member_Id;
    [self.navigationController pushViewController:vc animated:YES];
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
